<template>
  <div class="container">
    <div class="navigationBar" style="width: 98%;margin: 10px auto 0">
      <el-row :gutter="24">
        <!-- 搜索框 -->
<!--        <el-col :span="5">-->
<!--          <el-input v-model="searchText" placeholder="请输入问题" clearable @change="search"></el-input>-->
<!--        </el-col>-->

        <el-col :span="3">
          <el-input v-model="listQuery.title" placeholder="请输入问题" clearable @change="search"></el-input>
        </el-col>

        <el-col :span="2">
          <el-select v-model="listQuery.status" placeholder="状态">
            <el-option
              v-for="item in status_options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>

        <el-col  :span="16">
          <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </el-col>

        <el-col :span="3">
          <el-button type="success" @click="handleAdd" class="add">增加问题</el-button>
        </el-col>
      </el-row>
    </div>

    <el-table :data="questionList" style="width: 98%;margin: 10px auto 0" border :key="questionList.length"
              v-loading="loading"
              element-loading-text="拼命加载中....">

      <el-table-column label="问题标题" align="center">
        <template slot-scope="{row}">
          <span>{{row.title}}</span>
        </template>
      </el-table-column>

      <el-table-column label="问题答案(图片)" align="center">
        <template slot-scope="{row}">
          <img :src="row.image === '' ? null : row.image" alt="暂无图片" @click="bigImage(row.image)">
        </template>
      </el-table-column>

      <el-table-column label="问题答案(文本)" align="center">
        <template slot-scope="{row}">
          <span>{{row.answer}}</span>
        </template>
      </el-table-column>


      <el-table-column prop="tag" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status == 0 ? 'danger' : 'primary'" disable-transitions>
            {{scope.row.status == 0 ? '停用':'启用'}}
          </el-tag>
        </template>
      </el-table-column>

        <el-table-column label="操作" align="center">
          <template slot-scope="{row}">
            <el-button size="mini" type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button type="danger" @click="openDelete(row.question_id)" size="mini">删除</el-button>

          </template>
        </el-table-column>
    </el-table>

    <transition name="el-zoom-in-top">
      <div v-show="bigImageSrc !== null && bigImageSrc!==''" class="bigImg" @click="bigImage(null)">
        <img :src="bigImageSrc" style="width:auto; height:400px;border-radius:5px;">
      </div>
    </transition>

    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,15,20,30,50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>


    <add-question ref="showAddDialog"/>
    <edit-question ref="showEditDialog"/>

  </div>
</template>

<script>
  import store from '@/store'
  import {list,del} from '@/api/thk/cms/question'
  import addQuestion from './add'
  import editQuestion from './edit'

  export default {
    name: 'Question',
    components: {
      addQuestion,
      editQuestion
    },

    data() {
      return {
        // save questionList field
        // question_id":"问题id","title":"问题","answer":"答案", "sort_order":"问题排序", "status":"状态",
        questionList: [],
        // 按问题名称搜索问题
        searchText: '',

        // 保存完整的列表
        tempQuestionList: [],

        listQuery: {
          title: undefined,
          status:undefined,
          page: 1,
          pagesize: 10
        },

        bigImageSrc: null,


        total: undefined,

        status_options: [{
          value: '0',
          label: '停用'
        }, {
          value: '1',
          label: '启用'
        }, {
          value: '',
          label: '全部'
        }],
        loading: true,

      }
    },

    // created生命周期中调用查询question列表的方法
    created() {
      this.getQuestionList();
    },

    updated: function () {
      this.$nextTick(function () {
        this.loading = false
      })
    },

    methods: {
      // 查询question列表
      getQuestionList(){
        list(store.getters.token, this.listQuery).then(res =>{
          this.total = res.data.list.total
          this.questionList = res.data.list.data
          console.log(this.questionList)
        },error => {
          console.log(error)
        })
      },

      // 触发增加按钮弹出增加问题对话框
      handleAdd(){
        // arg：是否点击了确定按钮
        this.$refs.showAddDialog.cancelSubmit()
      },




      // 触发编辑按钮.row是一行的数据，点击了哪一行，就是哪一行的数据，根据数据回显
      handleEdit(row){
        this.$refs.showEditDialog.showEditDialog(row)
      },

      // 删除question
      handleDelete(question_id){
        del(store.getters.token, question_id).then(res => {
          // 删除之后，需要重新渲染.重新查询数据，会根据数据的length重新渲染
          this.getQuestionList()
          console.log("删除成功!",res)
        }).catch(err => {
          console.log("删除失败",err)
        })
      },

      // 弹出是否删除对话框, then中调用handleDelete
      openDelete(question_id) {
        this.$confirm('此操作将会删除问题, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(question_id)
          this.$message({
            type: 'success',
            message: '删除成功!'

          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      search(){
        this.listQuery.page = 1
        this.getQuestionList()
      },

      handleSizeChange(val) {
        this.listQuery.pagesize = val
        this.getQuestionList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getQuestionList()
      },

      // 大图弹窗函数
      bigImage(src) {
        if (src!==null) {
          this.$set(this, 'bigImageSrc', src)
          console.log(this.bigImageSrc)
        } else {
          this.$set(this, 'bigImageSrc', null)
        }
      },

    }
  }

</script>

<style scoped>
  img{
    height: 50px;
    width: 50px;
    margin: 0;
    padding: 0;
    border-radius: 4px;
  }
  .clear-fixed::after {
    content: "";
    clear: both;
    display: block;
  }
  .bigImg img{
    max-width: 600px;
    min-width: 150px;
    height:auto;
    border-radius:10px;
  }

  .bigImg{
    min-width:100%;
    min-height:100%;
    position:fixed;
    top:0;
    left:0;
    z-index:999999;
    background:rgba(0,0,0,.5);
    display:flex;
    justify-content:center;
    align-items:center;
    cursor:pointer;
    /*transition:1s;*/
  }
  .add{
    position: relative;
    left: -20px;
  }
</style>
